# 获取当天日期
import datetime
import time


class MDateTime():
    """
    基于datatime标准的时间处理
    """

    def __init__(self):
        pass

    def date_format(self, datestring, sourceformat='%b %d, %Y', tarformat="%Y%m%d"):
        """
        特定字符串转时间 转 string
        MAY 24, 2017  ==》 20170524
        :param datestring: 时间字符串
        :param sourceformat: 源格式 源格式需要与传入的字符串格式一致
        :param tarformat: 目标格式
        :return: 一个目标格式的string
        """
        """
        
        MAY 24, 2017  ==》 20170524
        :param datestring:
        :return:
        """
        return time.strftime(tarformat, time.strptime(datestring, sourceformat))

    @classmethod
    def get_now_time(self):
        """
        获取现在的时间
        :return: {
        type: datetime.datetime
        cmd python: datetime.datetime(2019, 1, 28, 17, 19, 5, 614144)
        print Strings: 2019-01-28 17:15:47.671929
        }
        """
        return datetime.datetime.now()

    def string_to_datetime(self, str, format="%Y-%m-%d %H:%M:%S.%f"):
        """
        string 转 datetime
        :param str: 2017-01-01 11:10:45.1234
        :param format:  格式 必须和str的格式相对相应
        :return:{
        type: datetime.datetime
        cmd python: datetime.datetime(2019, 1, 28, 17, 19, 5, 614144)
        print Strings: 2019-01-28 17:15:47.671929
        }
        """
        return datetime.datetime.strptime(str, format)

    def get_diff(self, datetime):
        """
        传入一个时间标准的str 求差秒
        :param datetime: {type: datetime.datetime}
        :return: string 秒
        """
        return (self.get_now_time() - self.string_to_datetime(datetime)).seconds

    @classmethod
    def get_now_datetime(cls, format="%Y%m%d%H%M%S", struct_time=time.localtime(time.time())):
        """
          获取当前时间 或指定时间 指定格式的时间字符串
        :param format:
        :param struct_time: type:time.struct_time
        :return: {
          type:string
        20190128172633
        }
        """
        return time.strftime(format, struct_time)

    def get_today_date(self):
        """
        获取当前日期级时间
        如 20180810
        :return: string
        """
        return self.get_now_datetime('%Y%m%d')

    def get_time(self):
        """
        返回当前时间的时间戳
        1546914500.7278442
        :return:
        """
        return time.time()

    def get_localtime(self, ct=time.time()):
        """
        传入一个时间戳
        :param times: 1546914500.7278442
        :return: time.struct_time(tm_year=2019, tm_mon=1, tm_mday=8, tm_hour=10, tm_min=29, tm_sec=46, tm_wday=1, tm_yday=8, tm_isdst=0)
        """
        return time.localtime(ct)

    @classmethod
    def get_msec_time(self):
        """
        得到毫秒级时间
        :return:
        """
        ct = time.time()
        local_time = time.localtime(ct)
        data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
        data_secs = (ct - int(ct)) * 1000
        time_stamp = "%s.%03d" % (data_head, data_secs)
        stamp = ("".join(time_stamp.split()[0].split("-")) + "".join(time_stamp.split()[1].split(":"))).replace('.', '')
        return time_stamp, stamp

    def get_next_month(self, datestring, day=1):
        """
        获取下一月的某一天
        :param datestring: 输入格式 20170524
        :return:  返回类型 datetime.datetime  格式  2019-01-01 00:00:00
        """
        return (self.string_to_datetime(datestring, "%Y%m%d") + datetime.timedelta(33)).replace(day=day)

    def get_day_sage(self, days, format="%Y%m%d"):
        """
        获取几天前的时间 并获取strings 可指定时间格式
        :param days:
        :return:
        """
        return (self.get_now_time() - datetime.timedelta(days)).strftime(format)

    @classmethod
    def get_day_after(cls, days, format="%Y%m%d"):
        """
        获取几天前的时间 并获取strings 可指定时间格式
        :param days:
        :return:
        """
        return (cls.get_now_time() + datetime.timedelta(days)).strftime(format)

    def get_now_month_start(self, datestring):
        """
        获取当月第一天
        :param datestring:
        :return:
        """
        return datetime.datetime.strptime(datestring, "%Y%m%d").replace(day=1)

    def date_to_string(self, date):
        """
        :param date: 输入类型 datetime.datetime
        :return: 2017.06.01
        """
        return date.strftime("%Y.%m.%d")

    def date_to_datetime(self, date):
        """
        由于datetime与date不能直接比较  所以需要转换一下
        :return:
        """
        return datetime.datetime.combine(date, datetime.datetime.min.time())

    def after_day(self, datestring):
        """
        获取后一天的时间 20181001
        :return:
        """
        tim = datetime.datetime.strptime(datestring, "%Y%m%d")
        return (tim + datetime.timedelta(days=+1)).strftime("%Y%m%d")  # 获取后一天 18-04-27 15:22:32

    def befor_day(self, datestring):
        """
        获取前一天的时间 20181001
        :return:
        """
        tim = datetime.datetime.strptime(datestring, "%Y%m%d")
        return (tim + datetime.timedelta(days=-1)).strftime("%Y%m%d")  # 获取后一天 18-04-27 15:22:32

    def befor_min(self, datestring):
        """
        获取前一天的时间 20181001
        :return:
        """
        tim = datetime.datetime.strptime(datestring, "%Y%m%d")
        return (tim - datetime.timedelta(days=-1)).strftime("%Y%m%d")  # 获取后一天 18-04-27 15:22:32


def getTodayDate():
    """
    获取当前日期级时间
    如 20180810
    :return: string
    """
    return time.strftime('%Y%m%d', time.localtime(time.time()))


def getDateTime():
    return time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))


def StringToDate(datestring):
    """
    特定字符串转时间
    MAY 24, 2017  ==》 20170524
    :param datestring:
    :return:
    """
    return time.strftime("%Y%m%d", time.strptime(datestring, '%b %d, %Y'))


def StringToDate2(datestring):
    """
    特定字符串转时间
    2017.05.24 ==》 20170524
    :param datestring:
    :return:
    """
    return time.strftime("%Y%m%d", time.strptime(datestring, '%Y.%m.%d'))


def getnextmonth(datestring, day=1):
    """
    获取下一月的某一天
    :param datestring: 输入格式 20170524
    :return:  返回类型 datetime.datetime  格式  2019-01-01 00:00:00
    """
    return (datetime.datetime.strptime(datestring, "%Y%m%d") + datetime.timedelta(33)).replace(day=day)


def getdaysage(days):
    return (datetime.datetime.now() - datetime.timedelta(days)).strftime("%Y%m%d")


def getnowmonthstart(datestring):
    """
    获取当月第一天
    :param datestring:
    :return:
    """
    return datetime.datetime.strptime(datestring, "%Y%m%d").replace(day=1)


def dateToString(date):
    """
    :param date: 输入类型 datetime.datetime
    :return: 2017.06.01
    """
    return date.strftime("%Y.%m.%d")


def dateTodateTime(date):
    """
    由于datetime与date不能直接比较  所以需要转换一下
    :return:
    """
    return datetime.datetime.combine(date, datetime.datetime.min.time())


def afterday(datestring):
    """
    获取后一天的时间 20181001
    :return:
    """
    tim = datetime.datetime.strptime(datestring, "%Y%m%d")
    return (tim + datetime.timedelta(days=+1)).strftime("%Y%m%d")  # 获取后一天 18-04-27 15:22:32


def beforday(datestring):
    """
    获取前一天的时间 20181001
    :return:
    """
    tim = datetime.datetime.strptime(datestring, "%Y%m%d")
    return (tim + datetime.timedelta(days=-1)).strftime("%Y%m%d")  # 获取后一天 18-04-27 15:22:32
